package main

import (
	"fmt"
	"lygin/ginSimple"
	"net/http"
	"reflect"
)

func newTestRouter() *ginSimple.Router {
	r := ginSimple.NewRouter()
	r.AddRouter("GET", "/", nil)
	r.AddRouter("GET", "/hello/:name", nil)
	r.AddRouter("GET", "/hello/b/c", nil)
	r.AddRouter("GET", "/hi/:name", nil)
	r.AddRouter("GET", "/assets/*filepath", nil)
	return r
}

func TestParsePattern() {
	ok := reflect.DeepEqual(ginSimple.ParsePattern("/p/:name"), []string{"p", ":name"})
	ok = ok && reflect.DeepEqual(ginSimple.ParsePattern("/p/*"), []string{"p", "*"})
	ok = ok && reflect.DeepEqual(ginSimple.ParsePattern("/p/*name/*"), []string{"p", "*name"})
	if !ok {
		fmt.Println("test parsePattern failed")
	}
}

func TestGetRoute() {
	r := newTestRouter()
	n, ps := r.GetRoute("GET", "/hello/ly")

	if n == nil {
		fmt.Println("nil shouldn't be returned")
	}

	if n.Pattern != "/hello/:name" {
		fmt.Println("should match /hello/:name")
	}

	if ps["name"] != "ly" {
		fmt.Println("name should be equal to 'ly'")
	}

	fmt.Printf("matched path: %s, params['name']: %s\n", n.Pattern, ps["name"])
}

func main() {

	//TestParsePattern()
	//TestGetRoute()

	r := ginSimple.New()
	r.Use(ginSimple.Logger())
	r.Use(ginSimple.Recover())
	//v1 := r.Group("/v1")
	//v1.GET("/", func(c *ginSimple.Context) {
	//	c.String(http.StatusOK, "hello ginSimple", c.Path)
	//})
	//v1.GET("/hello", func(c *ginSimple.Context) {
	//	c.String(http.StatusOK, "hello %s, you are at %s\n", c.Query("name"), c.Path)
	//})
	//
	//v2 := r.Group("/v2")
	//v2.GET("/hello/:name", func(c *ginSimple.Context) {
	//	c.String(http.StatusOK, "hello %s, you are at %s\n", c.Param("name"), c.Path)
	//})
	//v2.POST("/login", func(c *ginSimple.Context) {
	//	c.JSON(http.StatusOK, ginSimple.H{
	//		"code":     http.StatusOK,
	//		"username": c.PostForm("username"),
	//		"password": c.PostForm("password"),
	//	})
	//})
	r.GET("/", func(c *ginSimple.Context) {
		c.String(http.StatusOK, "Hello Geektutu\n")
	})
	r.GET("/panic", func(c *ginSimple.Context) {
		names := []string{"geektutu"}
		c.String(http.StatusOK, names[100])
	})

	r.Run(":8080")
}
